﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TrinitySequenceAssembly
{
    class SequenceReadParser
    {
        int m_KmerLength;
        public SequenceReadParser(int kmerLength)
        {
            m_KmerLength = kmerLength;
        }

        private string GetReverseComplementKmer(string kmer)
        {
            string reverseKmer = "";
            for (int i = kmer.Length - 1; i >= 0; --i)
            {
                switch (kmer[i])
                {
                    case 'A': reverseKmer += "T"; break;
                    case 'G': reverseKmer += "C"; break;
                    case 'C': reverseKmer += "G"; break;
                    case 'T': reverseKmer += "A"; break;
                    default: reverseKmer += "A"; break;
                }
            }
            return reverseKmer;
        }

        public DebruijnNode[] Parse(string sequenceRead, long sequenceReadID)
        {
            DebruijnNode[] res = null;
            if (sequenceRead.Length >= m_KmerLength)
            {
                int kmerNum = sequenceRead.Length - m_KmerLength + 1;
                res = new DebruijnNode[2 * kmerNum];
                for (int i = 0; i < kmerNum; )
                {
                    string kmer = sequenceRead.Substring(i, m_KmerLength);
                    long compressedKmer = KmerParser.CompressKmer(kmer);
                    DebruijnNode debruijnNode = new DebruijnNode(compressedKmer, sequenceReadID, i);
                    res[i] = debruijnNode;
                    i += 1;
                    string reverseKmer = GetReverseComplementKmer(kmer);
                    long reverseCompressedKmer = KmerParser.CompressKmer(kmer);
                    DebruijnNode reverseNode = new DebruijnNode(reverseCompressedKmer, -sequenceReadID, -i);
                    res[i] = reverseNode;
                    i += 1;
                }
            }
            return res;
        }
    }
}
